<?php
namespace App\Business\BLL;

use App\Business\IBLL\IPlay;
use App\Models\ChallengeGame;
use App\Models\Reservation;
use App\Models\WechatUser;
use Carbon\Carbon;

class Personal implements IPlay
{
    public $id;
    public $city;
    public $findId;

    public function __construct($findID)
    {
        $this->findId = $findID;
    }

    //挑战赛-A
    public function validateChallengeA()
    {
        $res = $this->verification();
        switch ($res['recode']) {
            case -1:
                return ['status' => false, 'msg' => '未找到挑战者'];
                break;
            case -2:
                return ['status' => false, 'msg' => '挑战者未认证手机'];
                break;
            case -3:
                return ['status' => false, 'msg' => '挑战者积分未达标'];
                break;
            default:
                break;
        }

        if (!$this->hasGame()) {
            return ['status' => false, 'msg' => '存在未完成比赛'];
        }
        return ['status' => true, 'msg' => '验证通过'];
    }
    //挑战赛-B
    public function validateChallengeB()
    {
        $res = $this->verification();
        switch ($res['recode']) {
            case -1:
                return ['status' => false, 'msg' => '未找到应战者'];
                break;
            case -2:
                return ['status' => false, 'msg' => '应战者未认证手机'];
                break;
            case -3:
                return ['status' => false, 'msg' => '应战者积分未达标'];
                break;
            default:
                return ['status' => true, 'msg' => '验证通过'];
                break;
        }

    }

    //预约-A
    public function validateReservationA($time)
    {
        $res = $this->verification();
        switch ($res['recode']) {
            case -1:
                return ['status' => false, 'msg' => '未找到挑战者'];
                break;
            case -2:
                return ['status' => false, 'msg' => '挑战者未认证手机'];
                break;
            case -3:
                return ['status' => false, 'msg' => '挑战者积分未达标'];
                break;
            default:
                break;
        }

        if (!$this->hasReservation($time)) {
            return ['status' => false, 'msg' => '2小时只能约1场'];
        }
        return ['status' => true, 'msg' => '验证通过'];
    }
    //预约-B
    public function validateReservationB($score = 0)
    {
        $res = $this->verification(true, $score);
        switch ($res['recode']) {
            case -1:
                return ['status' => false, 'msg' => '未找到应战者'];
                break;
            case -2:
                return ['status' => false, 'msg' => '应战人未认证手机'];
                break;
            case -3:
                return ['status' => false, 'msg' => '应战人积分未达标'];
                break;
            default:
                return ['status' => true, 'msg' => '验证通过'];
                break;
        }
    }

    private function verification($noCheckTele = true, $score = 0)
    {
        $play = WechatUser::where('id', $this->findId)
            ->orWhere('openid', strval($this->findId))
            ->first();

        if (!$play) {
            return ['recode' => -1, 'msg' => '未找到人员'];
        }

        if (!$noCheckTele && empty($play->tele)) {
            return ['recode' => -2, 'msg' => '电话未认证'];
        }
        if ($play->score < $score) {
            return ['recode' => -3, 'msg' => '积分未达标'];
        }

        $this->id = $play->id;
        $this->city = $play->city;

        return ['recode' => 1, 'msg' => '检查通过'];
    }

    private function hasGame()
    {
        //查询是否有比赛
        $game = ChallengeGame::where([['status', 10], ['play_a', $this->id]])->first();
        if ($game) {
            return false;
        }
        return true;
    }

    private function hasReservation($time)
    {
        $time = Carbon::parse($time);
        $start = $time->subHours(2);
        $end = $time->addHours(2);

        //查询当天是否存在挑战
        $game = Reservation::where('play_a', $this->id)
            ->whereIn('status', [10, 20])
            ->whereBetween('game_datetime', [$start, $end])
            ->first();
        if ($game) {
            return false;
        }
        return true;
    }
}
